'''
Created on 2009-11-20

@author: yuchen

This unittest is for testing whether the program can generate
a series of correct successor states,which including:

testStateSuccessorMove:
	The easiest condition that just initiating the game. 

testStateGenerate_chained_movesWithTurnKing: 
	A complex condition with about twenty possible moves

testStateGenerate_chained_movesWithTurnKingDiffcult:
	A more complex condition that can not appear in a real game.
	
testAIcomputerget_move:
	pass the successor states into the computer.py for evaluting the peformance
'''
import unittest, copy
import gamemodel.board
import gamemodel.player
import gamemodel.state
import gamemodel.piece
import interface.ConsoleAgent
import ai.computer
import ai.human
import game_model_test


class Test(unittest.TestCase):
	__board_for_test = None
	__player0 = None
	__player1 = None
	

	def testStateSuccessorMove(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		
#		If need, use the commands below to print the created board 
#		print
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		
		moves = state.successorMove()
		states = state.Successor()

#		If need, use the commands below to print all the states after moving
#		for state in states:
#			print "one of the choice is:"
#			interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
#			pieces = state.board.getPlayer0().pieces
#			print "the list of piece is:"
#			for piece in pieces:
#				print piece.position,
#			print
		
#       if need, use the commands below to print the feasible moves
#		for move in moves:
#			for micromove in move.list:
#				print "original is", micromove.original
#				print "destination is", micromove.destination
#			print "one branch end!"
#			print
#			print	 
		
		self.assertEqual(len(moves), 9) 
		self.assertEqual(len(states), 9)
		
		return 

	def testStateGenerate_chained_movesWithTurnKing(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		player0 = board.getPlayer0()
		player1 = board.getPlayer1()
		for i in range(10):
			j = 5
			if i % 2 == 1:
				piece = gamemodel.piece.CheckerPiece(player0, i, j)
				state.board.setBoardPosition(i, j, player0, piece)
				board.setBoardPosition(i, j, player0, piece)
				board.player0.add_piece(piece)
			
			board.deletePiece(i, 0, board.getPlayer1())
			board.deletePiece(i, 2, board.getPlayer1())
			board.deletePiece(i, 1, board.getPlayer1())
			
			
		piece = gamemodel.piece.CheckerPiece(player1, 3, 1)
		state.board.setBoardPosition(3, 1, player1, piece)
		board.setBoardPosition(3, 1, player1, piece)
		board.player1.add_piece(piece)
		
		piece = gamemodel.piece.CheckerPiece(player1, 1, 1)
		state.board.setBoardPosition(1, 1, player1, piece)
		board.setBoardPosition(1, 1, player1, piece)
		board.player1.add_piece(piece)

#		If need, use the commands below to print the created board		
#		print			
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)	
		
		moves = state.successorMove()		
		states = state.Successor()
		
#		If need, use the commands below to print all the states after moving
#		for state in states:			
#			print
#			interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
#			print "the length of player0.pieces is", len(state.players[0].pieces)
#			print "the length of player1.pieces is", len(state.players[1].pieces)
#			print "the list of player's pieces is:"
#			for piece in state.players[0].pieces:
#				print piece.position, piece.king
#				print
#			print
#			
#			for piece in state.players[1].pieces:
#				print piece.position, piece.king
#				print
#			print
		
#       if need, use the commands below to print the feasible moves
#		for move in moves:
#			for micromove in move.list:
#				print "original is", micromove.original
#				print "destination is", micromove.destination
#			print "one branch end!"
#			print
#			print	 
		
		self.assertEqual(len(moves), 2)
		self.assertEqual(len(states), 2)

		return

	def testStateGenerate_chained_movesWithTurnKingDiffcult(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		player0 = board.getPlayer0()
		player1 = board.getPlayer1()
		for i in range(10):
			j = 5
			if i % 2 == 1:
				piece = gamemodel.piece.CheckerPiece(player1, i, j)
				state.board.setBoardPosition(i, j, player1, piece)
				board.setBoardPosition(i, j, player1, piece)
				board.player1.add_piece(piece)
			
			board.deletePiece(i, 0, board.getPlayer1())
			board.deletePiece(i, 2, board.getPlayer1())
		
#		If need, use the commands below to print the created board		
#		print			
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		
		moves = state.successorMove()
		states = state.Successor()
		
		
#		If need, use the commands below to print all the states after moving
#		for state in states:			
#			print
#			interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
#			print "the length of player0.pieces is", len(state.players[0].pieces)
#			print "the length of player1.pieces is", len(state.players[1].pieces)
#			print "the list of player's pieces is:"
#			for piece in state.players[0].pieces:
#				print piece.position, piece.king
#				print
#			print
#			
#			for piece in state.players[1].pieces:
#				print piece.position, piece.king
#				print
#			print
		
#		if need, use the commands below to print the feasible moves
#		for move in moves:
#			for micromove in move.list:
#				print "original is", micromove.original
#				print "destination is", micromove.destination
#			print "one branch end!"
#			print
#			print	

		self.assertEqual(len(moves), 12)
		self.assertEqual(len(states), 12)
		
		return

	def testAIcomputerget_move(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		player0 = board.getPlayer0()
		player1 = board.getPlayer1()
		
		states = state.Successor()
		evaluatedValue = []
		
		for state in states:
			evaluatedValue.append(ai.computer.AIcomputer(player0).get_evaluation(state, 0))
#		
#		for evaluation in evaluatedValue :
#			print
#			interface.ConsoleAgent.ConsoleDescriptor.drawState(evaluation.state)
#			print "the list of piece is"
#			print len(evaluation.player.pieces)
#			for piece in evaluation.player.pieces :
#				print piece.position,
#			print
#			print "player is", evaluation.player 
#			print "otherPlayer is", evaluation.otherPlayer 
#			print "pieceAmount is", evaluation.pieceAmount
#			print "kingAmount  is", evaluation.kingAmount 
#			print "pieceDifference is",evaluation.pieceDifference
#			print "kingDifference is",evaluation.kingDifference
#			print "currentAverageY  is", evaluation.currentAverageY 
#			print "currentAgglomerationY is", evaluation.currentAgglomerationY 
#			print "currentAgglomerationX is", evaluation.currentAgglomerationX 
#			print "opponentAverageY is", evaluation.opponentAverageY
#			print "pponentAgglomerationY is", evaluation.opponentAgglomerationY 
#			print "opponentAgglomerationX is", evaluation.opponentAgglomerationX
#			print
#			print
#		
		self.assertEqual(len(evaluatedValue), 9)
		
		return

if __name__ == "__main__":
	#import sys;sys.argv = ['', 'Test.testName']
	unittest.main()